package team2.arsystem.GUI;

import java.util.ArrayList;

import team2.arsystem.Controller.ApplicationController;
import team2.arsystem.Utilities.CommandConstants;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;

public class ViewMembers extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		ScrollView sv = new ScrollView(this);
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		sv.addView(ll);

	
		TextView membersLabelTextView = new TextView(this);
		membersLabelTextView.setText("\n Friends added in your application \n");
		ll.addView(membersLabelTextView);

		/*Get the members that the user has already added in the application*/
		final ArrayList<ArrayList<String>> membersDetails= ApplicationController.getApplicationController().passCommandAndFetchMultipleResult(CommandConstants.viewMembers);
		
		final RadioGroup rg = new RadioGroup(this);
		
		final ArrayList<RadioButton> rb=new ArrayList<RadioButton>();

		 for(int i = 0; i < membersDetails.get(0).size(); i++) {
			
			rb.add(new RadioButton(this));
			rb.get(i).setText(membersDetails.get(0).get(i));
			rg.addView(rb.get(i));
			
		}
		 ll.addView(rg);
		
		
		LinearLayout innerll = new LinearLayout(this);
		innerll.setOrientation(LinearLayout.HORIZONTAL);
		ll.addView(innerll);
			
		Button editMemberButton = new Button(this);
		editMemberButton.setText("Edit Member");
		innerll.addView(editMemberButton);
		
		Button goBackButton = new Button(this);
		goBackButton.setText("Go Back");
		innerll.addView(goBackButton);
		
	
	
		this.setContentView(sv);
			
		editMemberButton.setOnClickListener(new OnClickListener() {
			
        	public void onClick(View v) {
        		
        	  	try
                 	{
                        	 Class menuScreen = Class.forName("team2.arsystem.GUI."+"EditMemberScreen");
                    		 Intent myIntent = new Intent(ViewMembers.this,menuScreen);
                    		 RadioButton selectedMember = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
                    		 myIntent.putExtra("team2.arsystem.GUI.ViewMembers.memberName", selectedMember.getText().toString());
                    		 String oldMobileNumber = "";
                    		 for(int k=0;k<membersDetails.get(0).size();k++)
                    		 {
                    			 Log.i("name",membersDetails.get(0).get(k).toString());
                    			 Log.i("number",membersDetails.get(1).get(k).toString());
                    			 
                    			 if(membersDetails.get(0).get(k).toString().equals(selectedMember.getText().toString()))
                    			 {
                    				 oldMobileNumber = membersDetails.get(1).get(k).toString();
                    			 }
                    		 }
                    		 
                    		 myIntent.putExtra("team2.arsystem.GUI.ViewMembers.memberMobileNumber", oldMobileNumber);
                    		 startActivity(myIntent);
                        	}
                        	catch(Exception e)
                        	{
                    			 e.printStackTrace();
                    		 }
                    		 
                        }
        	
                    });

		goBackButton.setOnClickListener(new OnClickListener() {
			
        	public void onClick(View v) {
        		
        	  	try
                 	{
                        	 Class menuScreen = Class.forName("team2.arsystem.GUI."+"MembersMenuScreen");
                    		 Intent myIntent = new Intent(ViewMembers.this,menuScreen);
                    		 startActivity(myIntent);
                        	}
                        	catch(Exception e)
                        	{
                    			 e.printStackTrace();
                    		 }
                    		 
                        }
        	
                    });
        		    
		
	}
	

}
